package thired.class03_structure;

import java.util.Stack;

public class Class03_StackToQueue {
    public static class MyQueue{
        private Stack<Integer> pushStack;
        private Stack<Integer> popStack;

        public MyQueue() {
            pushStack = new Stack<Integer>();
            popStack = new Stack<Integer>();
        }

        private  void pushToPop(){
            if(popStack.isEmpty()){
                while (!pushStack.isEmpty()){
                    popStack.push(pushStack.pop());
                }
            }
        }

        private void push(int val){
            pushStack.push(val);
            pushToPop();
        }

        private int pop(){
            if(pushStack.isEmpty() && popStack.isEmpty()){
                throw new RuntimeException("队列空");
            }
            pushToPop();
            return popStack.pop();
        }

        private int peek() {
            if (popStack.empty() && pushStack.empty()) {
                throw new RuntimeException("Queue is empty!");
            }
            pushToPop();
            return popStack.peek();
        }
    }

    public static void main(String[] args) {
        MyQueue test = new MyQueue();
        test.push(1);
        test.push(2);
        test.push(3);
        System.out.println(test.peek());
        System.out.println(test.pop());
        System.out.println(test.peek());
        System.out.println(test.pop());
        System.out.println(test.peek());
        System.out.println(test.pop());
    }
}
